import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:studymanage/core/sound/ui/remote_player.dart';
import 'package:studymanage/util/fun.dart';
import 'package:studymanage/util/request.dart';

class MyShare extends StatefulWidget {
  MyShare({Key key, this.title}) : super(key: key);

  final String title;

  @override
  State<StatefulWidget> createState() {
    return MyShareState();
  }
}

class MyShareState extends State<MyShare> {
  //已经点评
  List _remarkedList=[] ;
  ScrollController _scrollController = ScrollController();

  Timer _timer; // 播放计时器
  int _page =1;
  bool isLoading = false;
  @override
  void initState() {
    _scrollController.addListener(() {
      if(_scrollController.position.pixels >
          _scrollController.position.maxScrollExtent-20){
        print('滑动到了最底部');
        _getNoMark();
      }
    });
    _getNoMark();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          backgroundColor: Colors.white,
          title: Text("我的功课",style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),),
          leading: FlatButton(
            child: Icon(
              Icons.arrow_back_ios,
              color: Colors.grey,
            ),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: SingleChildScrollView(
          controller: _scrollController,
          child: Container(
            width: MediaQuery.of(context).size.width,
            color: Color(0xffF8F8F8),
            padding: EdgeInsets.only(
              bottom: ScreenUtil().setHeight(20),
              left: ScreenUtil().setWidth(12.5),
              right: ScreenUtil().setWidth(12.5),
            ),
            child: ListView.builder(
              padding: EdgeInsets.all(0),
              shrinkWrap: true,
              physics: NeverScrollableScrollPhysics(),
              itemCount: _remarkedList.length,
              itemBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.only(top:10),
                  padding: EdgeInsets.all(15),
                  color: Colors.white,
                  child: Column(
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.all(13),
                        decoration: BoxDecoration(
                            color: Color(0xffF8F8F8),
                            borderRadius: BorderRadius.circular(4)
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Container(
                                  height: 41,
                                  width: 41,
                                  child: CircleAvatar(
                                    backgroundImage: NetworkImage(_remarkedList[index]['studentAvatar']),
                                    radius: ScreenUtil().setWidth(20.5),
                                  ),
                                ),
                                Expanded(
                                  child: Container(
                                    margin: EdgeInsets.only(left: 10),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Row(
                                          children: <Widget>[
                                            Text(
                                              _remarkedList[index]['studentNickname'],
                                              style: TextStyle(
                                                fontFamily: 'PingFangSC-Medium',
                                                fontSize: ScreenUtil().setSp(14),
                                                fontWeight: FontWeight.w600,
                                                color: Color.fromRGBO(51, 51, 51, 1),
                                                height: 1,
                                              ),
                                            ),
                                            Offstage(
                                              offstage: _remarkedList[index]['className']==null,
                                              child: Container(
                                                width: 50,
                                                padding: EdgeInsets.all(3),
                                                margin: EdgeInsets.only(
                                                  left: ScreenUtil().setWidth(9.5),
                                                ),
                                                decoration: BoxDecoration(
                                                  border: Border.all(
                                                    color: Color.fromRGBO(133, 133, 133, 1),
                                                    width: ScreenUtil().setHeight(0.5),
                                                  ),
                                                  borderRadius: BorderRadius.all(
                                                    Radius.circular(ScreenUtil().setHeight(2)),
                                                  ),
                                                ),
                                                child: Text(
                                                  _remarkedList[index]['className']!=null ? _remarkedList[index]['className'] :  "",
                                                  overflow: TextOverflow.ellipsis,
                                                  style: TextStyle(
                                                    fontFamily: 'PingFangSC-Light',
                                                    fontSize: ScreenUtil().setSp(10),
                                                    fontWeight: FontWeight.w300,
                                                    color: Color.fromRGBO(133, 133, 133, 1),
                                                  ),
                                                ),
                                              ),
                                            ),
                                            Offstage(
                                              offstage: !_remarkedList[index]['boolComplete'],
                                              child: Container(
                                                padding: EdgeInsets.all(3),
                                                margin: EdgeInsets.only(
                                                  left: 3,
                                                ),
                                                decoration: BoxDecoration(
                                                  border: Border.all(
                                                    color: Color.fromRGBO(133, 133, 133, 1),
                                                    width: ScreenUtil().setHeight(0.5),
                                                  ),
                                                  borderRadius: BorderRadius.all(
                                                    Radius.circular(ScreenUtil().setHeight(2)),
                                                  ),
                                                ),
                                                child: Text(
                                                  "已经结业",
                                                  style: TextStyle(
                                                    fontFamily: 'PingFangSC-Light',
                                                    fontSize: ScreenUtil().setSp(10),
                                                    fontWeight: FontWeight.w300,
                                                    color: Color.fromRGBO(133, 133, 133, 1),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(top: 5),
                                          child: Row(
                                            children: <Widget>[
                                              Container(
                                                child: Text(
                                                  _remarkedList[index]['shareDateCN'],
                                                  style: TextStyle(
                                                    fontFamily: 'PingFangSC-Light',
                                                    fontSize: ScreenUtil().setSp(11),
                                                    fontWeight: FontWeight.w300,
                                                    color: Color.fromRGBO(133, 133, 133, 1),
                                                    height: 1,
                                                  ),
                                                ),
                                              ),
                                              RemotePlayer(_remarkedList[index]['shareRecordContentVoice'],2)
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                )],
                            ),
                            Container(
                              margin: EdgeInsets.only(left: 51,top: 10),
                              child: Text(_remarkedList[index]['shareRecordContent']!=null?_remarkedList[index]['shareRecordContent']:""),
                            )

                          ],
                        ),
                      ),
                      Offstage(
                        offstage: _remarkedList[index]['teacherContent']==null,
                        child: Container(
                          margin: EdgeInsets.only(top: 23),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: <Widget>[
                              Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: <Widget>[
                                  Column(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: <Widget>[
                                      Row(
                                        crossAxisAlignment: CrossAxisAlignment.center,
                                        children: <Widget>[
                                          Container(
                                            padding: EdgeInsets.only(
                                              left: ScreenUtil().setWidth(4.5),
                                              right: ScreenUtil().setWidth(4.5),
                                              top: ScreenUtil().setHeight(3.5),
                                              bottom: ScreenUtil().setHeight(3.5),
                                            ),
                                            margin: EdgeInsets.only(
                                              right: ScreenUtil().setWidth(9.5),
                                            ),
                                            decoration: BoxDecoration(
                                              border: Border.all(
                                                color: Color.fromRGBO(133, 133, 133, 1),
                                                width: ScreenUtil().setHeight(0.5),
                                              ),
                                              borderRadius: BorderRadius.all(
                                                Radius.circular(ScreenUtil().setHeight(2)),
                                              ),
                                            ),
                                            child: Text(
                                              '情感导师点评',
                                              style: TextStyle(
                                                fontFamily: 'PingFangSC-Light',
                                                fontSize: ScreenUtil().setSp(10),
                                                fontWeight: FontWeight.w300,
                                                color: Color.fromRGBO(133, 133, 133, 1),
                                              ),
                                            ),
                                          ),
                                          Text(
                                            _remarkedList[index]['teacherNickname']!=null ? _remarkedList[index]['teacherNickname'] : "" ,
                                            style: TextStyle(
                                              fontFamily: 'PingFangSC-Medium',
                                              fontSize: ScreenUtil().setSp(14),
                                              fontWeight: FontWeight.w600,
                                              color: Color.fromRGBO(51, 51, 51, 1),
                                              height: 1,
                                            ),
                                          ),
                                        ],
                                      ),
                                      Container(
                                        margin: EdgeInsets.only(top: 5),
                                        child: Row(
                                          children: <Widget>[
                                            Container(
                                              margin: EdgeInsets.only(right: 8),
                                              child: Text(
                                                _remarkedList[index]['commentDateCN']!=null?_remarkedList[index]['commentDateCN']:"",
                                                style: TextStyle(
                                                  fontFamily: 'PingFangSC-Light',
                                                  fontSize: ScreenUtil().setSp(11),
                                                  fontWeight: FontWeight.w300,
                                                  color: Color.fromRGBO(133, 133, 133, 1),
                                                  height: 1,
                                                ),
                                              ),
                                            ),
                                            Offstage(
                                              offstage: !_remarkedList[index].containsKey("teacherContentVoice"),
                                              child: RemotePlayer(_remarkedList[index]['teacherContentVoice'],2),
                                            )
                                          ],
                                        ),
                                      )
                                    ],
                                  ),
                                  Container(
                                    height: 41,
                                    width: 41,
                                    margin: EdgeInsets.only(left: 10),
                                    child: CircleAvatar(
                                      backgroundImage: NetworkImage(_remarkedList[index]['teacherAvatar']!=null?_remarkedList[index]['teacherAvatar']:""),
                                      radius: ScreenUtil().setWidth(20.5),
                                    ),
                                  ),
                                ],
                              ),
                              Container(
                                margin: EdgeInsets.only(right: 51,top: 10,left: 51),
                                alignment: Alignment.topRight,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.end,
                                      children: <Widget>[
                                        Text(_remarkedList[index]['teacherContent']!=null?_remarkedList[index]['teacherContent']:""),
                                        Container(
                                            height: 0,
                                            child: Text(_remarkedList[index]['courseNmae']!=null ? _remarkedList[index]['courseNmae'] : "" ,style: TextStyle(fontSize: ScreenUtil().setSp(11),),)
                                        ),

                                      ],
                                    ),
                                    Container(
                                      decoration: BoxDecoration(
                                          border: Border.all(
                                            color: Color.fromRGBO(133, 133, 133, 1),
                                            width: ScreenUtil().setHeight(0.5),
                                          ),
                                          borderRadius: BorderRadius.circular(2)
                                      ),
                                      margin: EdgeInsets.only(top: 10),
                                      padding: EdgeInsets.all(3),
                                      child: Text(
                                        _remarkedList[index]['courseNmae']!=null ? _remarkedList[index]['courseNmae'] : "",
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(
                                          fontFamily: 'PingFangSC-Regular',
                                          fontSize: ScreenUtil().setSp(11),
                                          color: Color.fromRGBO(133, 133, 133, 1),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),

                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }



  _getNoMark() {
    if(isLoading){
      return;
    }
    if(_timer!=null){
      _timer.cancel();
      _timer = null;
    }
    setState(() {
      isLoading = true;
    });
    Request().post<Map<String, dynamic>>("/multiapi/c226p_studyRecord",
        data: {
          "pageNum": _page,
          "pageSize": 10,
        }).then((response){
      response.data['list'].forEach((element) {
        element['isPlayAudio'] = false;
        element['initSchedule'] = 0;
        element['currentSchedule'] = 0.0;
        element['teacherIsPlayAudio'] = false;
        element['teacherInitSchedule'] = 0;
        element['teacherCurrentSchedule'] = 0.0;
      });
      setState(() {
        if(!response.data["isLastPage"]){
          isLoading =false;
        }
        _page+=1;
        _remarkedList.addAll(response.data['list']) ;
      });
    });
  }
}
